/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.TextArea;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;


public class TextAreaRenderer extends FormItemRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        TextArea formField = (TextArea) itemToRender;
        StringBuffer html = new StringBuffer("");
        Object obj = formField.getModelBean();

        if (obj == null) {
            obj = (String) evaluate(pCtx, formField, formField.getDefaultValue());
        }

        String value = (obj == null) ? "" : obj.toString();

        if (formField.isHidden()) {
            return hiddenField(pCtx, formField, value);
        }

        if (isVisible(pCtx, formField)) {
            html.append(textAreaField(pCtx, formField, value));
        } else {
            html.append("<INPUT type =\"hidden\" name=\"" + formField.getFieldName() + "\"")
                .append(addTagProperty(" value", value, null)).append(">");
        }

        return html;
    }

    private String textAreaField(WebContext pCtx, TextArea formField, String value) {
        StringBuffer html = new StringBuffer("");
        value = brtocr(value);

        if (formField.isEnabled()) {
            html.append("<TEXTAREA " + "name=\"" + formField.getFieldName() + "\"")
                .append(getEditScript(pCtx, formField)).append(addTagProperty("style", formField.getStyle(pCtx), null))
                .append(addTagProperty("class", formField.getStyleClass(), null)).append(">")
                .append((value == null) ? "" : value).append("</TEXTAREA>");
        } else {
            html.append("<DIV ").append(getEditScript(pCtx, formField)).append(addTagProperty("nowrap", "", null))
                .append(addTagProperty("style", formField.getStyle(pCtx), null)).append(">").append(value).append("&nbsp;</DIV>");
        }

        if (pCtx.isError()) {
            html.append(renderError(pCtx, formField));
        }

        return html.toString();
    }
}
